package com.sds.sweng.swat.miplatform.ui.design.policies;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.LayoutEditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

import com.sds.sweng.swat.miplatform.ui.design.models.commands.CopyTableCellCommand;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MTableComponents;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.sub.TableCellSubAreaPart;

public class TableCellLayoutEditPolicy extends LayoutEditPolicy {

	protected Command getAddCommand(Request request) {
		ChangeBoundsRequest req = (ChangeBoundsRequest) request;
		List<EditPart> editParts = req.getEditParts();

		if (editParts == null || editParts.size() == 0)
			return null;

		if (!(editParts.get(0) instanceof TableCellSubAreaPart))
			return null;

		if (!(getHost().getModel() instanceof MTableComponents))
			return null;

		TableCellSubAreaPart host = (TableCellSubAreaPart) getHost();
		TableCellSubAreaPart guest = (TableCellSubAreaPart) editParts.get(0);

		MTableComponents hostModel = (MTableComponents) host.getModel();
		MTableComponents guestModel = (MTableComponents) guest.getModel();

		if (hostModel.getWidth() < guestModel.getComponentsWidth())
			return null;

		CopyTableCellCommand command = new CopyTableCellCommand(hostModel, guestModel.getComponents());

		return command;
	}

	protected EditPolicy createChildEditPolicy(EditPart child) {
		return new NonResizableEditPolicy();
	}

	@Override
	protected Command getCreateCommand(CreateRequest request) {
		return null;
	}

	@Override
	protected Command getMoveChildrenCommand(Request request) {
		return null;
	}

}
